/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projet.gestionnaire;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import projetweb.modeles.Client;

/**
 *
 * @author mankou
 */
@Stateless
public class GestionnaireClients {
@PersistenceContext

        private EntityManager em;  
      
        public void creerclientDeTest() throws UnsupportedEncodingException {  
            creeClient("John", "Lennon", "jlennon", "ff","fdff");  
            creeClient("Paul", "Mac Cartney", "pmc", "gg","fghhtd");  
            creeClient("Ringo", "Starr", "rstarr", "hh","wsgln");  
            creeClient("Georges", "Harisson", "georgesH", "kk","xlflk");  
        }  
      
        public Client creeClient(String firstname, String lastname, String login, String pass,String adress) throws UnsupportedEncodingException {  
           Client u = new Client(firstname, lastname, login, pass,adress);  
            em.persist(u);  
            return u;  
        }  
      
        public Collection<Client> getAllclient() {  
            // Exécution d'une requête équivalente à un select *  
            Query q = em.createQuery("select u from Client u");  
            return q.getResultList();  
        }  
        
        public Collection<Client> rechercheClient(String login){
            Query q = em.createQuery("select clients from Client clients where clients.login =:param");
            q.setParameter("param", login);
       
            return q.getResultList();
        }
        
        public void supprimerClient(String login){
            Query q =em.createQuery("delete from Client Clients where Clients.login =:param");
            q.setParameter("param", login);
            q.executeUpdate();
               
        }
        
        public void updateClient(String login, String pwd, String nom, String prenom, Object adress){
            Query q =em.createQuery("update Client Clients set Clients.password =:param2, Clients.nom = :param3, Clients.prenom = :param4,Clients.adress = :param5 where Clients.login =:param1"); 
            q.setParameter("param1", login);
            q.setParameter("param2", pwd);
            q.setParameter("param3", nom);
            q.setParameter("param4", prenom);
            q.setParameter("param5", adress);
            q.executeUpdate();    
        }
        
        public Collection<Client> getAllClient(int start){
            Query q = em.createQuery("select clients from Client clients");
            q.setMaxResults(10);
            q.setFirstResult(start);
            return q.getResultList();
   }
        
        public long getNbClient(){
            Query q = em.createQuery("select count(clients) from Client clients");
            return (Long)q.getSingleResult();
        }
        // Add business logic below. (Right-click in editor and choose  
        // "Insert Code > Add Business Method")  

    public Collection<Client> getAllClient() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    }  
